package rtdb.pas;

import com.ht.aems.fesmodel.CommUnit;
import com.ht.aems.fesmodel.HisDataProvider;
import com.ht.aems.fesmodel.PointManager;
import com.ht.aems.fesmodel.RtMeasureQuery;
import com.ht.aems.fesmodel.data.AnalogPoint;
import com.ht.aems.fesmodel.data.DiscretePoint;
import com.ht.aems.fesmodel.persist.HisDataSqlPersist;
import org.apache.log4j.Logger;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Dong Shufeng
 * Date: 11-10-27
 */
public class PasPointsLoader {
    private static Logger log = Logger.getLogger(SeFesPoints.class);

    public static void loadPoints(PasFesPoints points) {
        PointManager pointManager = new PointManager(points.getRtuName());
        List<CommUnit> commUnits = pointManager.getCommUnits();
        if (commUnits == null || commUnits.size() < 1) {
            log.debug("No virtual rtu is found.");
            return;
        } else if (commUnits.size() > 1) {
            log.debug("More than one virtual rtu are found.");
            return;
        }
        CommUnit commUnit = commUnits.get(0);
        Field[] fields = points.getClass().getFields();
        for (Field f : fields) {
            String pointName = points.getPointName(f.getName());
            if (pointName.equals(""))
                continue;
            if (f.getType() == DiscretePoint.class && pointManager.containsYX(pointName)) {
                try {
                    f.set(points, pointManager.getDiscretePoint(pointName));
                } catch (IllegalAccessException e) {
                    log.warn(e);
                }
            } else if (f.getType() == AnalogPoint.class && pointManager.containsYC(pointName)) {
                try {
                    f.set(points, pointManager.getAnalogPoint(pointName));
                } catch (IllegalAccessException e) {
                    log.warn(e);
                }
            }
        }
        points.setCommUnit(commUnit);
    }

    public static void loadPointValuesFromRtdb(PasFesPoints points, RtMeasureQuery rtMeasureQuery) {
        for (Field f : points.getAnalogs()) {
            AnalogPoint p;
            try {
                p = (AnalogPoint) f.get(points);
            } catch (IllegalAccessException e) {
                log.warn(e);
                continue;
            }
            if (p == null)
                continue;
            Long ycTime = rtMeasureQuery.getYCTime(p.getId());
            if (ycTime == null)
                continue;
            p.setTimestamp(new Date(ycTime));
            Double ycValue = rtMeasureQuery.getYCValue(p.getId());
            if (ycValue != null)
                p.setEngValue(ycValue);
        }
        for (Field f : points.getDiscretes()) {
            DiscretePoint p;
            try {
                p = (DiscretePoint) f.get(points);
            } catch (IllegalAccessException e) {
                log.warn(e);
                continue;
            }
            if (p == null)
                continue;
            Long yxTime = rtMeasureQuery.getYXTime(p.getId());
            if (yxTime == null)
                continue;
            p.setTimestamp(new Date(yxTime));
            Integer yxValue = rtMeasureQuery.getYXValue(p.getId());
            if (yxValue != null)
                p.setEngValue(yxValue);
        }
    }

    public static void loadPointValuesFromHisdb(PasFesPoints points) {
        HisDataProvider hisDataProvider = new HisDataSqlPersist();
        for (Field f : points.getDiscretes()) {
            DiscretePoint p;
            try {
                p = (DiscretePoint) f.get(points);
            } catch (IllegalAccessException e) {
                log.warn(e);
                continue;
            }
            if (p == null)
                continue;
            Integer v = hisDataProvider.get(p, System.currentTimeMillis());
            if (v == null)
                continue;
            p.setTimestamp(new Date(System.currentTimeMillis()));
            p.setEngValue(v);
        }
        for (Field f : points.getAnalogs()) {
            AnalogPoint p;
            try {
                p = (AnalogPoint) f.get(points);
            } catch (IllegalAccessException e) {
                log.warn(e);
                continue;
            }
            if (p == null)
                continue;
            Double v = hisDataProvider.get(p, System.currentTimeMillis());
            if (v == null)
                continue;
            p.setTimestamp(new Date(System.currentTimeMillis()));
            p.setEngValue(v);
        }
    }
}
